<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Search.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\community;

use app\api\model\dao\Circle;
use app\api\model\dao\Note;
use app\api\model\dao\WordLibrary;
use app\api\model\data\circle\CircleData;
use app\api\model\data\note\NoteData;
use app\library\Common;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Log;
use think\Validate;

class SearchCircle extends PageBase
{

    private $objCircle;
    private $objNote;
    private $objNoteData;
    private $objWordLibrary;
    private $objCircleData;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objNote = new Note();
        $this->objWordLibrary = new WordLibrary();
        $this->objNoteData = new NoteData();
        $this->objCircleData = new CircleData();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'wd' => 'require',
        ];
        $msg = [
            'wd.require' => '请输入查询内容',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        $ret = $this->checkParam($param);
        if ($ret) {
            Log::error(__METHOD__ . ' param error. [param: ' . json_encode($param) . ']');
            return $this->getRes(Error::ERR_PARAM);
        }

        $wd = $param['wd'];

        $conds = [
            'name' => ['like', '%' . $wd . '%'],
            'approve_status' => ['=', Flag::CONST_CIRCLE_APPROVE_PASS],
            'is_delete' => ['=', Flag::CONST_NOTE_DELETE_NOT],
        ];

        $circle_ret = $this->objCircle->getCircle($conds);

        $this->data['list'] = $this->objCircleData->composeCircle($circle_ret);

        return $this->getRes();

    }
}
